//BookServiceImpl.java
package com.ylaihui.service.impl;

import com.sun.deploy.panel.ITreeNode;
import com.ylaihui.dao.BookDao;
import com.ylaihui.dao.impl.BookDaoImpl;
import com.ylaihui.pojo.Book;
import com.ylaihui.pojo.Page;
import com.ylaihui.service.BookService;

import java.math.BigDecimal;
import java.util.List;

public class BookServiceImpl implements BookService {

    private BookDao bookDao = new BookDaoImpl();

    @Override
    public void addBook(Book book) {
        bookDao.addBook(book);
    }

    @Override
    public void deleteBookById(Integer id) {
        bookDao.deleteBookById(id);
    }

    @Override
    public void updateBook(Book book) {
        bookDao.updateBook(book);
    }

    @Override
    public Book queryBookById(Integer id) {
        return bookDao.queryBookById(id);
    }

    @Override
    public List<Book> queryBooks() {
        return bookDao.queryBooks();
    }

    /**
     * 查询某一页的数据
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> pagevar = new Page<Book>();
        // 设置当前页， 每页数量
        //pagevar.setPageNo(pageNo);
        pagevar.setPageSize(pageSize);

        //总记录数
        Integer pageTotalCount = bookDao.queryTotalCount();
        pagevar.setPageTotalCount(pageTotalCount);

        //总页数
        int pageTotal = pageTotalCount / pageSize;
        if( pageTotalCount % pageSize > 0 ) {
            pageTotal += 1;
        }
        pagevar.setPageTotal(pageTotal);

        // 如果用户在浏览器地址栏输入非法数据，那么处理下边界
        //http://127.0.0.1:8080/book/manager/bookServlet?action=page&pageNo=-1
        if(pageNo < 1) pageNo = 1;
        if (pageNo > pageTotal) pageNo = pageTotal;
        pagevar.setPageNo(pageNo);

        //查询当前页的数据
        int begin = (pageNo - 1) * pageSize;
        List<Book> items = bookDao.queryPageItems(begin ,pageSize);

        pagevar.setItems(items);

        return pagevar;
    }

    @Override
    public Page<Book> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Book> pagevar = new Page<Book>();
        // 设置当前页， 每页数量
        //pagevar.setPageNo(pageNo);
        pagevar.setPageSize(pageSize);

        //总记录数
        Integer pageTotalCount = bookDao.queryTotalCountByPrice(min, max);
        pagevar.setPageTotalCount(pageTotalCount);

        //总页数
        int pageTotal = pageTotalCount / pageSize;
        if( pageTotalCount % pageSize > 0 ) {
            pageTotal += 1;
        }
        pagevar.setPageTotal(pageTotal);

        // 如果用户在浏览器地址栏输入非法数据，那么处理下边界
        //http://127.0.0.1:8080/book/manager/bookServlet?action=page&pageNo=-1
        if(pageNo < 1) pageNo = 1;
        if (pageNo > pageTotal) pageNo = pageTotal;
        pagevar.setPageNo(pageNo);

        //查询当前页的数据
        int begin = (pageNo - 1) * pageSize;
        List<Book> items = bookDao.queryPageItemsByPrice(begin ,pageSize, min, max);

        pagevar.setItems(items);

        return pagevar;
    }
}
